package com.tebiecloud.autoconfigure;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自定义公共字段自动填充
 * @Author: tebie
 * @Date: 2019-08-08 11:49
 */
@Slf4j
@Component
public class MetaObjectHandlerConfiguration implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        if(metaObject.hasSetter("ct")) {
            this.setInsertFieldValByName("ct", new Date(), metaObject);
        }
        if(metaObject.hasSetter("ut")) {
            this.setUpdateFieldValByName("ut", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
        if(metaObject.hasSetter("ut")) {
            this.setUpdateFieldValByName("ut", new Date(), metaObject);
        }
    }

}
